package cn.itguy.mobileguard.activity;

import cn.itguy.mobileguard.ActivityJumper;
import cn.itguy.mobileguard.IMGPreferences;
import cn.itguy.mobileguard.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ConfigActivity extends BaseActivity implements
		OnCheckedChangeListener {

	private IMGPreferences preferences;

	private RelativeLayout allclear_method_bar;
	private TextView tv_allclear_method;
	private RelativeLayout set_pwd_bar;
	private TextView tv_set_pwd;
	private RelativeLayout set_gesture_bar;
	private TextView tv_set_gesture;
	private RelativeLayout set_audio_bar;
	private TextView tv_set_audio;
	private RelativeLayout set_count_dialog_bar;
	private TextView tv_set_count_dialog;
	private RadioGroup rg_sensitivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		preferences = IMGPreferences.getInstance(this);

		setContentView(R.layout.config);

		((TextView) findViewById(R.id.tv_title))
				.setText(R.string.titleText_config);

		Button btn_back = (Button) findViewById(R.id.btn_left);
		btn_back.setVisibility(View.VISIBLE);
		btn_back.setOnClickListener(this);

		allclear_method_bar = (RelativeLayout) findViewById(R.id.allclear_method_bar);
		allclear_method_bar.setOnClickListener(this);

		set_pwd_bar = (RelativeLayout) findViewById(R.id.set_pwd_bar);
		set_pwd_bar.setOnClickListener(this);

		set_gesture_bar = (RelativeLayout) findViewById(R.id.set_gesture_bar);
		set_gesture_bar.setOnClickListener(this);

		set_audio_bar = (RelativeLayout) findViewById(R.id.set_audio_bar);
		set_audio_bar.setOnClickListener(this);

		set_count_dialog_bar = (RelativeLayout) findViewById(R.id.set_count_dialog_bar);
		set_count_dialog_bar.setOnClickListener(this);

		tv_allclear_method = (TextView) findViewById(R.id.tv_allclear_method);
		tv_set_pwd = (TextView) findViewById(R.id.tv_set_pwd);
		tv_set_gesture = (TextView) findViewById(R.id.tv_set_gesture);
		tv_set_audio = (TextView) findViewById(R.id.tv_set_audio);
		tv_set_count_dialog = (TextView) findViewById(R.id.tv_set_count_dialog);

		int allclearIndex = preferences.getAllclearMethodIndex();
		setAllclearMethod(allclearIndex);

		int alarmRingIndex = preferences.getAlarmRingIndex();
		setAlarmRing(alarmRingIndex);

		int time = preferences.getCountDialogTime();
		setCountDialogTime(time);

		rg_sensitivity = (RadioGroup) findViewById(R.id.rg_sensitivity);
		rg_sensitivity.setOnCheckedChangeListener(this);
		((RadioButton) rg_sensitivity.getChildAt(preferences
				.getSensorSensitivity())).setChecked(true);

	}

	private void setCountDialogTime(int time) {
		String[] times = getResources().getStringArray(R.array.time_items);
		SpannableStringBuilder builder = new SpannableStringBuilder();
		int timeIndex = 1;
		switch (time) {
		case 0:
			timeIndex = 0;
			break;
		case 3:
			timeIndex = 1;
			break;
		case 5:
			timeIndex = 2;
			break;
		case 10:
			timeIndex = 3;
			break;
		}
		builder.append(times[timeIndex]);
		builder.setSpan(new TextAppearanceSpan(this,
				R.style.TextViewTheme_Small_Grey), 0, builder.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv_set_count_dialog.setText(R.string.contentText_set_count_dialog);
		tv_set_count_dialog.append(builder);
	}

	private void setAlarmRing(int alarmRingIndex) {
		String[] rings = getResources().getStringArray(R.array.ring_items);
		SpannableStringBuilder builder = new SpannableStringBuilder();
		builder.append(rings[alarmRingIndex]);
		builder.setSpan(new TextAppearanceSpan(this,
				R.style.TextViewTheme_Small_Grey), 0, builder.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv_set_audio.setText(R.string.itemText_audio);
		tv_set_audio.append(builder);
	}

	private void setAllclearMethod(int allclearIndex) {
		String[] methods = getResources().getStringArray(
				R.array.allclear_method_items);
		SpannableStringBuilder builder = new SpannableStringBuilder();
		if (allclearIndex == 2) {
			builder.append(methods[0].concat(", ").concat(methods[1]));
		} else {
			builder.append(methods[allclearIndex]);
		}
		builder.setSpan(new TextAppearanceSpan(this,
				R.style.TextViewTheme_Small_Grey), 0, builder.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv_allclear_method.setText(R.string.contentText_allclear_method);
		tv_allclear_method.append(builder);

		switch (allclearIndex) {
		case 0:
			set_pwd_bar.setClickable(true);
			set_gesture_bar.setClickable(false);
			tv_set_pwd.setTextColor(getResources().getColor(R.color.black));
			tv_set_gesture.setTextColor(getResources().getColor(R.color.gray));
			break;
		case 1:
			set_pwd_bar.setClickable(false);
			set_gesture_bar.setClickable(true);
			tv_set_pwd.setTextColor(getResources().getColor(R.color.gray));
			tv_set_gesture.setTextColor(getResources().getColor(R.color.black));
			break;
		case 2:
			set_pwd_bar.setClickable(true);
			set_gesture_bar.setClickable(true);
			tv_set_pwd.setTextColor(getResources().getColor(R.color.black));
			tv_set_gesture.setTextColor(getResources().getColor(R.color.black));
			break;
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			ActivityJumper.toIMobileGuardActivity(this);
			finish();
			break;
		case R.id.allclear_method_bar:
			showSelectAllclearMethodDialog();
			break;
		case R.id.set_pwd_bar:
			ActivityJumper.toSetPasswordActivity(this);
			break;
		case R.id.set_gesture_bar:
			ActivityJumper.toGestureBuilderActivity(this);
			break;
		case R.id.set_audio_bar:
			showSelectAudioDialog();
			break;
		case R.id.set_count_dialog_bar:
			showSelectCountDialogTimeDialog();
			break;
		}
	}

	private void showSelectCountDialogTimeDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.titleText_select_count_dialog_time)
				.setItems(R.array.time_items,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								int time = 3;
								switch (which) {
								case 0:
									time = 0;
									break;
								case 1:
									time = 3;
									break;
								case 2:
									time = 5;
									break;
								case 3:
									time = 10;
									break;
								}
								preferences.setCountDialogTime(time);
								setCountDialogTime(time);
							}
						}).show();
	}

	private void showSelectAudioDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.titleText_select_ring)
				.setItems(R.array.ring_items,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								preferences.setAlarmRingIndex(which);
								setAlarmRing(which);
							}
						}).show();
	}

	private void showSelectAllclearMethodDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.titleText_select_allclear_method)
				.setItems(R.array.allclear_method_items,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								preferences.setAllclearMethodIndex(which);
								setAllclearMethod(which);
							}
						}).show();
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rb_high:
			preferences.setSensorSensitivity(2);
			break;
		case R.id.rb_low:
			preferences.setSensorSensitivity(0);
			break;
		case R.id.rb_middle:
			preferences.setSensorSensitivity(1);
			break;
		}
	}

}
